<?php
/**************************************
 *
 * @Repositories 数据层~主要与数据库打交道
 *
 **************************************/
namespace app\common\repositories;
use app\common\model\IndexModel;

use think\Model;

use think\Db;

class IndexRepositories
{
    protected $IndexModel;

    public function __construct()
    {
        $this->IndexModel = new IndexModel();
    }

    /*
     *  首页文章列表
     */
    public function getList($requestData)
    {
        if (isset($requestData['id'])) {
            $map['a.id'] = array('lt',$requestData['id']);
        }
        $user_id = null;
        if (isset($requestData['user_id'])) {
            $user_id = $requestData['user_id'];
        }
        $map['a.status'] = 1;
        $item = IndexModel::alias('a')
                ->join('special b', 'a.sp_id = b.id', 'LEFT')
                ->field('a.id, a.sp_id, a.img,a.title, b.title sp_title, b.img sp_img')
                ->where($map)
                ->order('a.id desc')
                ->limit(5)
                ->select();
//        foreach ($item as &$v) {
//            $v->img = basename($v->img);
//        }
        $data = [];
        //获取文章列表点赞量
        $clickRepositories = new ClickRepositories();
        for ($i=0; $i<count($item);$i++) {
            $data[$i]['id'] = $item[$i]->id;
            $data[$i]['user_id'] = $user_id;
        }
        $clickNum = $clickRepositories->clickNum($data);
        $num = count($item);
        $arr = [];
        for ($i=0; $i<count($item);$i++) {
            $item[$i]->click = $clickNum[$i]['click'];
            $item[$i]->whether = $clickNum[$i]['whether'];
        }
        if ($item) {
            return $item;
        } else {
            return false;
        }
    }

    /*
     *  文章详情
     */
    public function getInfo($param)
    {
        $user_id = null;
        if (isset($param['user_id'])) {
            $user_id = $param['user_id'];
        }
        $item = Db::table('article')
                    ->alias('a')
                    ->join('special b', 'a.sp_id = b.id', 'LEFT')
                    ->field('a.*, b.title sp_title, b.desc sp_desc, b.img sp_img')
                    ->where(['a.id' => $param['id']])
                    ->find();
        $item['str_count'] = strlen($item['content']);
        //获取点赞量
        $data[0]['id'] = $item['id'];
        $data[0]['user_id'] = $user_id;
        $clickRepositories = new ClickRepositories();
        $clickNum = $clickRepositories->clickNum($data);
        if ($clickNum) {
            $item['click'] = $clickNum[0]['click'];
            $item['whether'] = $clickNum[0]['whether'];
        }
        //获取点赞人头
        $userImg = $clickRepositories->getClickImg($data);
        if ($userImg) {
            $item['user_img'] = $userImg[0]['click'];
        }
        //文章浏览量
        $view_num = $clickRepositories->getView($param['id']);
        if ($view_num) {
            $item['view'] = $view_num;
        }
        if ($item) {
            return $item;
        } else {
            return false;
        }
    }

    /*
     *  相关阅读
     */
    public function relRead($requestData)
    {
        $user_id = null;
        if (isset($requestData['user_id'])) {
            $user_id = $requestData['user_id'];
        }
        $map['status'] = 1;
        $map['sp_id'] = $requestData['sp_id'];
        $map['id'] = array('neq', $requestData['id']);
        $item = Db::table('article')->where($map)->order('rand()')->limit(3)->select();
        $data = [];
        //获取文章列表点赞量
        $clickRepositories = new ClickRepositories();
        for ($i=0; $i<count($item);$i++) {
            $data[$i]['id'] = $item[$i]['id'];
            $data[$i]['user_id'] = $user_id;
        }
        $clickNum = $clickRepositories->clickNum($data);
        if ($clickNum) {
            for ($i=0; $i<count($item);$i++) {
                $item[$i]['click'] = $clickNum[$i]['click'];
                $item[$i]['whether'] = $clickNum[$i]['whether'];
            }
        }
        //人脑壳
        $userImg = $clickRepositories->getClickImg($data);
        if ($userImg) {
            for ($i=0; $i<count($item);$i++) {
                $item[$i]['user_img'] = $userImg[$i]['click'];
            }
        }
        if ($item) {
            return $item;
        } else {
            return false;
        }

    }
}